package com.feilong.edgeAI.remind.room.ui;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.feilong.edgeAI.R;
import com.feilong.edgeAI.remind.alarm.AlarmClockHelper;
import com.feilong.edgeAI.remind.alarm.AlarmReceiver;
import com.feilong.edgeAI.remind.room.adapter.RemindListAdapter;
import com.feilong.edgeAI.remind.room.bean.RemindBean;
import com.feilong.edgeAI.remind.room.db.RemindDatabase;
import com.feilong.edgeAI.ui.RemindActivity;
import com.github.gzuliyujiang.calendarpicker.CalendarPicker;
import com.github.gzuliyujiang.calendarpicker.OnSingleDatePickListener;
import com.github.gzuliyujiang.wheelpicker.DatimePicker;
import com.github.gzuliyujiang.wheelpicker.OptionPicker;
import com.github.gzuliyujiang.wheelpicker.SexPicker;
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode;
import com.github.gzuliyujiang.wheelpicker.annotation.TimeMode;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatimePickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionSelectedListener;
import com.github.gzuliyujiang.wheelpicker.entity.DatimeEntity;
import com.github.gzuliyujiang.wheelpicker.widget.DatimeWheelLayout;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.qqtheme.framework.util.DateUtils;

public class AddRemindActivity extends AppCompatActivity implements View.OnClickListener, OnOptionPickedListener {

    private EditText mMessageEdit;
    private TextView tv_time;
    private RecyclerView mRecyclerView;
    // 获取AlarmManager服务

    private TextView tv_cycle;
    private String strCycleMode = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_remind);

        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        getWindow().setStatusBarColor(Color.TRANSPARENT);

        initView();
    }

    private void initView() {
        tv_time = findViewById(R.id.tv_insert_time);
        mMessageEdit = findViewById(R.id.insert_message_edit);
        tv_cycle = findViewById(R.id.tv_cycle);
        mRecyclerView = findViewById(R.id.data_list_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        findViewById(R.id.query_button).setOnClickListener(this);
        findViewById(R.id.insert_button).setOnClickListener(this);
        findViewById(R.id.alarm_button).setOnClickListener(this);
        findViewById(R.id.tv_insert_time).setOnClickListener(this);
        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.tv_cycle).setOnClickListener(this);
    }



    private long alarmTimeStamp;
    private String roomDay, roomMinute, roomTime,roomO0;
    private int alarmHour, alarmMinute;


    public void onYearMonthDayTime() {
        DatimePicker picker = new DatimePicker(this);
        final DatimeWheelLayout wheelLayout = picker.getWheelLayout();
        picker.setOnDatimePickedListener(new OnDatimePickedListener() {
            @Override
            public void onDatimePicked(int year, int month, int day, int hour, int minute, int second) {
                alarmHour = hour;
                alarmMinute = minute;

                roomDay = year + "-" + month + "-" + day;

                if (minute < 10) {
                    roomMinute = hour + ":0" + minute ;
                } else {
                    roomMinute = hour + ":" + minute;
                }

                if (second < 10){
                    roomMinute = roomMinute+":0"+second ;
                }else {
                    roomMinute = roomMinute +second ;
                }
                Log.i("AAAAA", "onDatimePicked: "+roomMinute);


                String text = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

                 String text2 = year + "-" + month + "-" + day + " " + hour + ":" + minute;
                //text += wheelLayout.getTimeWheelLayout().isAnteMeridiem() ? " 上午" : " 下午";

                Date date = DateUtils.parseDate(text);
                alarmTimeStamp = date.getTime();
                tv_time.setText("" + text2);

            }
        });

        wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY);
        wheelLayout.setTimeMode(TimeMode.HOUR_24_NO_SECOND);
        wheelLayout.setRange(DatimeEntity.now(), DatimeEntity.yearOnFuture(10));
        picker.getWheelLayout().setStyle(R.style.WheelDefault);
        wheelLayout.setDateLabel("年", "月", "日");
        wheelLayout.setTimeLabel("时", "分", "秒");
        picker.show();
    }

    @Override
    public void onClick(View v) {
        String mTime = tv_time.getText().toString();
        String mMessage = mMessageEdit.getText().toString();
        switch (v.getId()) {
            case R.id.query_button:
                //queryPhone();
                queryRemind();
                break;
            case R.id.insert_button:
                if (mTime.isEmpty()) {
                    Toast.makeText(this, "时间不能为空", Toast.LENGTH_SHORT).show();
                } else if (mMessage.isEmpty()) {
                    Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    tv_time.setText("");
                    mMessageEdit.setText("");

                    insertRemind(mTime, mMessage, alarmTimeStamp);
                    setAlarm(alarmTimeStamp, mMessage);
                    startActivity(new Intent(AddRemindActivity.this, RemindActivity.class));
                    finish();
                }
                break;

            case R.id.alarm_button:

                break;
            case R.id.tv_insert_time:
                onYearMonthDayTime();
                break;
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.tv_cycle:
                onOptionText();
                break;
        }
    }


    private void setAlarm(long alarmTime, String message) {
        //AlarmClockHelper.setAlarmClock(getApplicationContext(), alarmTime);
        AlarmClockHelper.setCirculateAlarmClock(getApplicationContext(),alarmTime,strCycleMode);
        Log.i("AAAA", "setAlarm: mode "+strCycleMode);
    }

    public void onOptionText() {
        OptionPicker picker = new OptionPicker(this);
        picker.setData("永不", "每小时", "每天", "每周", "每月", "每年");
        picker.setOnOptionPickedListener(this);
        picker.getWheelLayout().setOnOptionSelectedListener(new OnOptionSelectedListener() {
            @Override
            public void onOptionSelected(int position, Object item) {
                tv_cycle.setText("" + item);
            }
        });
        //picker.getWheelView().setStyle(R.style.WheelStyleDemo);
        picker.show();
    }

    /**
     * 添加数据到数据库
     *
     * @param mTime
     * @param mMessage
     */
    private void insertRemind(String mTime, String mMessage, long timeStamp) {

        List<RemindBean> remindBeans = new ArrayList<>();
        RemindBean remindBean = new RemindBean(
                0, "a", strCycleMode, "a", "a", "a",
                "a", roomDay, roomMinute, timeStamp, "a", mTime,
                "a", mMessage, "a", "a", "a",
                "a"
        );
        remindBeans.add(remindBean);
        RemindDatabase.getDefault(getApplicationContext()).getRemindDao().insertAll(remindBeans);
    }


    private void queryRemind() {
        List<RemindBean> remindBeanList = RemindDatabase.getDefault(getApplicationContext()).getRemindDao().getRemindAll();
        RemindListAdapter mAdapter = new RemindListAdapter(remindBeanList);
        mRecyclerView.setAdapter(mAdapter);


        mAdapter.setOnItemClickListener(new RemindListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RemindBean remindBean) {
                Bundle mBundle = new Bundle();
                mBundle.putParcelable("RemindBean", remindBean);
                EditDialog mDialog = new EditDialog();
                mDialog.show(AddRemindActivity.this, mBundle, "edit");
                mDialog.setOnRefreshDataListener(new EditDialog.OnRefreshDataListener() {
                    @Override
                    public void onRefresh() {
                        //cancelAlarm();
                        queryRemind();
                    }
                });

            }
        });
    }

    @Override
    public void onOptionPicked(int position, Object item) {
        tv_cycle.setText("" + item);
        String strCycle = (String) item;
        switch (strCycle) {
            case "永不":
                strCycleMode = "0";
                break;
            case "每小时":
                strCycleMode = "1";
                break;
            case "每天":
                strCycleMode = "2";
                break;
            case "每周":
                strCycleMode = "3";
                break;
            case "每月":
                strCycleMode = "4";
                break;
            case "每年":
                strCycleMode = "5";
                break;

        }
    }

    public void back(View view) {
        finish();
    }
}
